//keydown事件表示键盘被按下
$(document).keydown(function(event){
    switch(event.keyCode){
        case 37://left
            /*
            moveLeft()方法
                *完成向左移动的逻辑
                *返回值是Boolean类型，判断是否可以继续向左移动
            */
            if(moveLeft()){
                //生成一个随机数
                setTimeout("generateOneNumber()",250)
                //判断游戏是否结束
                isgameover();
            }
            break;
        
        case 38://up
            if(moveUp()){
                setTimeout("generateOneNumber()",250)
                isgameover();
            }
            break;
        
        case 39://right
            if(moveRight()){
                setTimeout("generateOneNumber()",250)
                isgameover();
            }
            break;
        
        case 40://down
            if(moveDown()){
                setTimeout("generateOneNumber()",250)
                isgameover();
            }
            break;
        default:
            break;
    }
})

function moveLeft() {
    //返回值是Boole类型，判断是否可以向左移动
    if(!canMoveLeft(board)){
        //当前的格子无法移动
        return false;
    }
    
    //完成向左移动的逻辑
    for(var i=0;i<4;++i){
        for(var j=1;j<4;j++){
            //当前数字格是有值的
            if(board[i][j] != 0){
                //向左移动
                for(var k=0;k<j;++k){
                    if(board[i][k] == 0 && noBlokHorizontalCol(i,k,j,board)) {
                        //才能向左移动
                        showMoveAnimation(i,j,i,k);
                        board[i][k] = board[i][j];
                        board[i][j] = 0;
                    }else if(board[i][k] == board[i][j] && noBlokHorizontalCol(i,k,j,board) && !hasConflicted[i][k]){
                        //才能向左移动

                        //move
                        showMoveAnimation(i,j,i,k);
                        //add
                        board[i][k] = board[i][j] * 2;
                        board[i][j] = 0;

                        //add score
                        score += board[i][k];
                        updateScore(score);

                        hasConflicted[i][k] = true;
                    }

                }
            }
        }
    }

    setTimeout("updateBoardView()",250);
    return true;
}

function moveUp() {
    //返回值是Boole类型，判断是否可以向左移动
    if(!canMoveUp(board)){
        //当前的格子无法移动
        return false;
    }
    
    for(var j=0;j<4;++j){
        for(var i=1;i<4;i++){
            //当前数字格是有值的
            if(board[i][j] != 0){
               //向上移动
                for(var k=0;k<i;++k){
                    if(board[k][j] == 0 && noBlokVerticalCol(j,k,i,board)) {
                        //才能向上移动
                        showMoveAnimation(i,j,k,j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;
                    }else if(board[k][j] == board[i][j] && noBlokVerticalCol(j,k,i,board) && !hasConflicted[k][j]){
                        //才能向上移动

                        //move
                        showMoveAnimation(i,j,k,j);
                        //add
                        board[k][j] += board[i][j];
                        board[i][j] = 0;

                         //add score
                         score += board[k][j];
                         updateScore(score);

                         hasConflicted[k][j] = true; 
                    }

                }
            }
        }
    }
    setTimeout("updateBoardView()",250);
    return true;
}

function moveRight() {
    //返回值是Boole类型，判断是否可以向左移动
    if(!canMoveRight(board)){
        //当前的格子无法移动
        return false;
    }
    
    for(var i=0;i<4;++i){
        for(var j=2;j>=0;j--){
            //当前数字格是有值的
            if(board[i][j] != 0){
               //向右移动
                for(var k=3;k>j;k--){
                    if(board[i][k] == 0 && noBlokHorizontalCol(i,j,k,board)) {
                        //才能向右移动
                        showMoveAnimation(i,j,i,k);
                        board[i][k] = board[i][j];
                        board[i][j] = 0;
                    }else if(board[i][k] == board[i][j] && noBlokHorizontalCol(i,j,k,board) && !hasConflicted[i][k]){
                        //才能向右移动

                        //move
                        showMoveAnimation(i,j,i,k);
                        //add
                        board[i][k] += board[i][j];
                        board[i][j] = 0;

                         //add score
                         score += board[i][k];
                         updateScore(score);

                        hasConflicted[i][k] =true;
                    }

                }
            }
        }
    }
    setTimeout("updateBoardView()",250);
    return true;
}

function moveDown() {
    //返回值是Boole类型，判断是否可以向左移动
    if(!canMoveDown(board)){
        //当前的格子无法移动
        return false;
    }
    
    for(var j=0;j<4;++j){
        for(var i=2;i>=0;i--){
            //当前数字格是有值的
            if(board[i][j] != 0){
               //向下移动
                for(var k=3;k>i;--k){

                    if(board[k][j] == 0 && noBlokVerticalCol(j,i,k,board)) {
                        //才能向上移动
                        showMoveAnimation(i,j,k,j);
                        board[k][j] = board[i][j];
                        board[i][j] = 0;
                    }else if(board[k][j] == board[i][j] && noBlokVerticalCol(j,i,k,board) && !hasConflicted[k][j]){
                        //才能向上移动

                        //move
                        showMoveAnimation(i,j,k,j);
                        //add
                        board[k][j] = board[i][j] * 2;
                        board[i][j] = 0;

                         //add score
                         score += board[k][j];
                         updateScore(score);

                         hasConflicted[k][j] = true;
                    }
                }
            }
        }
    }
    setTimeout("updateBoardView()",250);
    return true;
}


function isgameover() {
    if(nospace(board) && nomove(board)) {
        gameover();
    }
}

function gameover() {
    // $("#gird-container").append("<div id='gameover' class='gameover'><p>本次得分</P><span>"+score+"</span><a href='javascript:restartgame();' id='restartgamebutton'>Restart</a></div>");

    // var gameover = $("#gameover");
    // gameover.css = ("width","500px");
    // gameover.css = ("height","500px");
    // gameover.css = ("background-color","rgba(0,0,0,0.5)");
    alert("dasd");
}